import uix
import uiutil
import xtriui
import uiconst
import uicls
import blue

class WindowButton(uicls.Container):
    __guid__ = 'uicls.WindowButton'

    def ApplyAttributes(self, attributes):
        uicls.Container.ApplyAttributes(self, attributes)
        wnd = attributes.wnd
        self.sr.label = uicls.Label(text='', parent=self, fontsize=10, letterspace=1, left=8, top=4, state=uiconst.UI_DISABLED, uppercase=1)
        self.sr.btn = uicls.Container(blue.os.LoadObject('res:/UI/Component/wnd_dockbutton.blue'))
        self.sr.btn.top = 0
        self.sr.btn.state = uiconst.UI_DISABLED
        self.children.append(self.sr.btn)
        self.sr.icon = None
        self.wr.wnd = wnd
        self.SetIcon(wnd.headerIconNo, 12)
        self.SetLabel(wnd.GetCaption())
        self.SetBlink(0)
        self.SetHighlight(0)
        self.sr.hint = wnd.headerIconHint



    def _OnClick(self, *args):
        wnd = self.wr.wnd
        sm.GetService('window').NotMinimized(self)
        eve.Message('ListEntryClick')
        if ((self is not None) and (not self.destroyed)):
            self.Close()
        if wnd:
            wnd.Maximize(expandIfCollapsed=0)



    def GetMenu(self, *args):
        if ((self.wr.wnd is not None) and (not self.wr.wnd.destroyed)):
            return uicls.Window.GetMenu(self.wr.wnd)
        else:
            return []



    def _OnMouseEnter(self, *args):
        self.SetHighlight(1)



    def _OnMouseExit(self, *args):
        self.SetHighlight(0)



    def SetLabel(self, labeltext):
        labeltext = labeltext.strip()
        if (len(labeltext) > 32):
            labeltext = ('%s...' % labeltext[:32])
        self.sr.label.text = labeltext
        self.width = ((self.sr.label.width + self.sr.label.left) + 8)
        self.sr.btn.width = self.width



    def SetIcon(self, iconNo, shiftLabel = 16, hint = None):
        if self.sr.icon:
            self.sr.icon.Close()
        if (iconNo is None):
            self.sr.label.left = 8
        else:
            self.sr.icon = uicls.Icon(icon=iconNo, parent=self, pos=(4, 2, 16, 16), align=uiconst.RELATIVE, idx=0, state=uiconst.UI_DISABLED)
            self.sr.label.left = (8 + shiftLabel)
        self.sr.hint = hint
        self.width = ((self.sr.label.width + self.sr.label.left) + 8)
        sm.GetService('window').RefreshWindowBtns()



    def SetBlink(self, blink = 1):
        uiutil.GetChild(self, 'blink').state = [uix.UI_HIDDEN, uix.UI_DISABLED][blink]



    def SetHighlight(self, hilite = 1):
        uiutil.GetChild(self, 'hilite').state = [uix.UI_HIDDEN, uix.UI_DISABLED][hilite]




